package com.hornsun.data.repository;

import com.hornsun.data.constant.types.Whether;
import com.hornsun.data.dbo.Role;
import com.hornsun.data.dbo.RoleExample;
import com.hornsun.data.repository.mapper.RoleMapper;
import com.hornsun.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by 编译中... on 2018/8/3.
 */
@Repository
public class RoleRepository {
    @Autowired
    private RoleMapper roleMapper;

    public List<Role> getTotalRole(){
        RoleExample roleExample  = new RoleExample();
        roleExample.setOrderByClause(StrUtil.toDesc("create_time"));
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andWhetherSuperEqualTo((byte) Whether.NOT.getType().intValue());
        return roleMapper.selectByExample(roleExample);
    }

    public void add(Role role){
        if (roleMapper.insertSelective(role)!=1){
            throw new IllegalArgumentException("insert role failed by role"+role);
        }
    }
    public void delete(String guid){
        if (roleMapper.deleteByPrimaryKey(guid)!=1){
            throw new IllegalArgumentException("delete role failed by guid"+guid);
        }
    }

    public void update(Role role){
        if (roleMapper.updateByPrimaryKeySelective(role)!=1){
            throw new IllegalArgumentException("update role failed by role"+role);
        }
    }

    public Role getByGuid(String guid){
        return roleMapper.selectByPrimaryKey(guid);
    }

    /**
     * 获取用户权限
     * @param roleGuid
     * @return
     */
    public int getPermission(String roleGuid){
        Role role = roleMapper.selectByPrimaryKey(roleGuid);
        if (role==null){
            return 0;
        }else {
            return role.getPermission();
        }
    }

}
